package com.lovestudying.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.util.CourseUtil;
import com.lovestudying.util.DBUtil;

@WebServlet("/AccountServlet")
public class AccountServlet extends HttpServlet {
	Gson gson = new Gson();
	/**
	 * authod Linzhengxiang
	 */
	private static final long serialVersionUID = 8708248841564314840L;

	/**
	 * @return
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");  
		String sql = "SELECT\r\n" + "	count(account_state) AS `value`,\r\n"
				+ "(case account_state  when 0 then '未注册用户'  when 1 then '已注册用户'  else '三无用户'  end  ) `name`\r\n"
				+ "\r\n" + "FROM\r\n" + "	account\r\n" + "GROUP BY\r\n" + "	account_state";
		ResultSet resultSet = CourseUtil.find(sql);
		try {
			response.setCharacterEncoding("UTF-8");
			List<Map<String, Object>> list = CourseUtil.ResultSetToList(resultSet);
			PrintWriter out = response.getWriter();
			out.write(gson.toJson(list));
			out.flush();
			out.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
